package ch.exploadingelephants.graph;

import java.util.List;

import ch.exploadingelephants.model.ConveyorDirection;

/**
 * @author Sandro De Zanet
 * 
 */
public class StartToken extends Token {

    private Token child;

    /*
     * (non-Javadoc)
     * 
     * @see
     * ch.exploadingelephants.graph.Token#accept(ch.exploadingelephants.graph
     * .DotString)
     */
    @Override
    public TokenResult accept(DotString inputPattern,
	    List<VisitedNodeState> visited) throws InfinityLoopException {
	checkForInfiniteLoop(inputPattern, visited);
	if (child != null) {
	    return child.accept(inputPattern, visited);
	} else {
	    return new TokenResult(false, inputPattern.copy());
	}
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * ch.exploadingelephants.graph.Token#connect(ch.exploadingelephants.graph
     * .Token)
     */
    public void connect(Token child) {
	this.child = child;
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * ch.exploadingelephants.graph.Token#tryConnect(ch.exploadingelephants.
     * graph.Token, ch.exploadingelephants.model.ConveyorDirection)
     */
    @Override
    public boolean tryConnect(Token createGraphObject,
	    ConveyorDirection direction) {
	if (ConveyorDirection.SOUTH.equals(direction)) {
	    connect(createGraphObject);
	    return true;
	}
	return false;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
	return "StartToken >> " + this.child;
    }
}
